package com.ruoyi.web.controller.project.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.project.domain.TOrderRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.controller.project.mapper.TVehicleMapper;
import com.ruoyi.web.controller.project.domain.TVehicle;
import com.ruoyi.web.controller.project.service.ITVehicleService;

/**
 * 车辆信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-27
 */
@Slf4j
@Service
public class TVehicleServiceImpl implements ITVehicleService 
{
    @Autowired
    private TVehicleMapper tVehicleMapper;

    /**
     * 查询车辆信息
     * 
     * @param vehicleId 车辆信息主键
     * @return 车辆信息
     */
    @Override
    public TVehicle selectTVehicleByVehicleId(String vehicleId)
    {
        return tVehicleMapper.selectTVehicleByVehicleId(vehicleId);
    }

    /**
     * 查询车辆信息列表
     * 
     * @param tVehicle 车辆信息
     * @return 车辆信息
     */
    @Override
    public List<TVehicle> selectTVehicleList(TVehicle tVehicle)
    {
        return tVehicleMapper.selectTVehicleList(tVehicle);
    }

    /**
     * 新增车辆信息
     * 
     * @param tVehicle 车辆信息
     * @return 结果
     */
    @Override
    public int insertTVehicle(TVehicle tVehicle)
    {
        return tVehicleMapper.insertTVehicle(tVehicle);
    }

    /**
     * 修改车辆信息
     * 
     * @param tVehicle 车辆信息
     * @return 结果
     */
    @Override
    public int updateTVehicle(TVehicle tVehicle)
    {
        return tVehicleMapper.updateTVehicle(tVehicle);
    }

    /**
     * 批量删除车辆信息
     * 
     * @param vehicleIds 需要删除的车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteTVehicleByVehicleIds(String[] vehicleIds)
    {
        return tVehicleMapper.deleteTVehicleByVehicleIds(vehicleIds);
    }

    /**
     * 删除车辆信息信息
     * 
     * @param vehicleId 车辆信息主键
     * @return 结果
     */
    @Override
    public int deleteTVehicleByVehicleId(String vehicleId)
    {
        return tVehicleMapper.deleteTVehicleByVehicleId(vehicleId);
    }

    @Override
    public String importVehicleList(List<TVehicle> tVehicleList, boolean updateSupport, String operName, Long userId)
    {
        if (StringUtils.isNull(tVehicleList) || tVehicleList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        log.debug("updateSupport={}",updateSupport);
        if (updateSupport){
            tVehicleMapper.deleteTVehicleByUserId(userId);
        }
        for (TVehicle vehicle : tVehicleList)
        {
            try
            {
                TVehicle selectedVehicle = tVehicleMapper.selectTVehicleByVehicleNumber(vehicle.getVehicleNumber());

                if (selectedVehicle == null){
                    TVehicle vehicleTemp = new TVehicle();
                    vehicleTemp.setVehicleNumber(vehicle.getVehicleNumber());
                    vehicleTemp.setStatus(vehicle.getStatus());
                    vehicleTemp.setVehicleDate(vehicle.getVehicleDate());
                    vehicleTemp.setUserId(userId);
                    vehicleTemp.setCreatedAt(new Date());
                    tVehicleMapper.insertTVehicle(vehicleTemp);
                    successNum++;
                    successMsg.append("<br/> " + successNum + "条记录" + " 导入成功");
                }else{
                    TVehicle vehicleTemp = new TVehicle();
                    vehicleTemp.setVehicleNumber(vehicle.getVehicleNumber());
                    vehicleTemp.setStatus(vehicle.getStatus());
                    vehicleTemp.setVehicleDate(vehicle.getVehicleDate());
                    vehicleTemp.setUserId(userId);
                    vehicleTemp.setUpdatedAt(new Date());
                    tVehicleMapper.updateTVehicle(vehicleTemp);
                    successNum++;
                    successMsg.append("<br/> " + successNum + "条记录" + " 更新导入成功(存在车辆编号)");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> " + failureNum + "条记录" + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
